package handler

import (
	"svvenus/conf"
	"svvenus/util/uttype"
	"svvenus/vgame/logic"
	"svvenus/znet"
	"svvenus/znet/zpb"
)

// CGMailListGet
// @Description: 邮件列表
// @param request
func CGMailListGet(request *znet.Request) {
	rsp := &zpb.MailInfoList{}
	rid := zpb.EPid_GCMailListGet

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家未登录, rsp)
		return
	}

	rsp = player.MailInfoList()

	request.Send(rid, 0, rsp)
}

// CGMailRead
// @Description: 读取邮件
// @param request
func CGMailRead(request *znet.Request) {
	rid := zpb.EPid_GCMailRead
	var req zpb.MsgString
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.SendCode(rid, conf.EProtocolResult玩家未登录)
		return
	}

	mail, ok := player.Mails[req.Value]
	if !ok {
		request.SendCode(rid, conf.EProtocolResult对象ID错误)
		return
	}
	mail.Read = true
	mail.NeedSave = true

	player.PushInfoMisc()
	request.SendCode(rid, 0)
}

// CGMailGet
// @Description: 领取邮件
// @param request
func CGMailGet(request *znet.Request) {
	rsp := &zpb.PbAwards{}
	rid := zpb.EPid_GCMailGet
	var req zpb.MsgString
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家未登录, rsp)
		return
	}
	mail, ok := player.Mails[req.Value]
	if !ok {
		request.Send(rid, conf.EProtocolResult对象ID错误, rsp)
		return
	}
	if mail.Got {
		request.Send(rid, conf.EProtocolResult邮件已领取, rsp)
		return
	}
	mail.Read = true
	mail.Got = true
	mail.NeedSave = true
	rsp.Awards = make([]*zpb.PbAward, 0)
	for _, v := range mail.Awards {
		rsp.Awards = append(rsp.Awards, logic.PcAward2PbAward(v))
	}
	player.SendAwards(mail.Awards, zpb.RecSource(mail.RecSource))

	player.PushInfo()
	request.Send(rid, 0, rsp)
}

// CGMailDelete
// @Description: 邮件删除
// @param request
func CGMailDelete(request *znet.Request) {
	rid := zpb.EPid_GCMailDelete
	var req zpb.MsgString
	if !request.GetPb(&req) {
		return
	}

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.SendCode(rid, conf.EProtocolResult玩家未登录)
		return
	}
	_, ok := player.Mails[req.Value]
	if !ok {
		request.SendCode(rid, conf.EProtocolResult对象ID错误)
		return
	}
	delete(player.Mails, req.Value)
	player.MailIds = uttype.SliceStringDelItem(player.MailIds, req.Value)
	go player.MailDelData(req.Value)

	player.PushInfo()
	request.SendCode(rid, 0)
}

// CGMailGetAll
// @Description: 一键领取
// @param request
func CGMailGetAll(request *znet.Request) {
	rsp := &zpb.PbAwards{}
	rid := zpb.EPid_GCMailGetAll

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家未登录, rsp)
		return
	}
	for _, mail := range player.Mails {
		if mail.Got {
			continue
		}
		mail.Read = true
		mail.Got = true
		mail.NeedSave = true
		rsp.Awards = make([]*zpb.PbAward, 0)
		for _, v := range mail.Awards {
			rsp.Awards = append(rsp.Awards, logic.PcAward2PbAward(v))
		}
		player.SendAwards(mail.Awards, zpb.RecSource(mail.RecSource))
	}

	player.PushInfo()
	request.Send(rid, 0, rsp)
}

// CGMailDeleteAll
// @Description: 一键删除已读
// @param request
func CGMailDeleteAll(request *znet.Request) {
	rsp := &zpb.PbAwards{}
	rid := zpb.EPid_GCMailGetAll

	player := logic.ConnGetPlayer(request.GetConnection())
	if player == nil {
		request.Send(rid, conf.EProtocolResult玩家未登录, rsp)
		return
	}
	listDelete := make([]string, 0)
	for _, mail := range player.Mails {
		if !mail.Read || !mail.Got {
			continue
		}
		listDelete = append(listDelete, mail.IdString())
	}
	for _, v := range listDelete {
		delete(player.Mails, v)
		go player.MailDelData(v)
	}
	player.MailIds = uttype.SliceStringDelItems(player.MailIds, listDelete)

	player.PushInfo()
	request.Send(rid, 0, rsp)
}
